#!/sbin/sh
#
# Create an HTML index from the installed Freeware products based
# on the release notes.
#
# usage: $0 [-installed|-web]
# -installed 	create index.html and index for installed products.
# -web		create web pages (i.e. leave out installed.html)
# -web is designed for internal SGI use only; generate pages for silicon surf.
#
# -installed is the default
#
# input: $COMMON/index.template, $COMMON/*.data
# output: $RELDIR/index.html, $RELDIR/installed.html, $RELDIR/*.html
#

# Generic template sections.
TITLE="TITLE"
HIGHLIGHTS="HIGHLIGHTS"
SIDENAV="SIDENAV"
CONTENTS="CONTENTS"

# Freeware-specific sections.
INST_ED="INSTALLED INDEX"
INST_ABLE="INSTALLABLE INDEX"
HTTP_LINK="HTTPLINKS"
FILE_LINK="FILELINKS"


#######################
## begin subroutines ##
#######################

# rmsection $input_file $section
rmsection()
{
    sed -e "/<!-- BEGIN $2 -->/,/<!-- END $2 -->/d" <$1 >$TMP
    mv -f $TMP $1
}

# applyfilters $input_file
applyfilters()
{
    [ true = $RMINSTED ]   && rmsection $1 "$INST_ED"
    if [ ! -f $RELDIR/index-by-alpha.html ]; then  ## fw_latest is not available
        [ true = $RMINSTABLE ] && rmsection $1 "$INST_ABLE"
    fi
    [ true = $RMHTTPLINK ] && rmsection $1 "$HTTP_LINK"
    [ true = $RMFILELINK ] && rmsection $1 "$FILE_LINK"
}


# mergetemplate $template $data $result
mergetemplate()
{
    template=$1
    data=$2
    result=$3

    rm -f $result $TMP

    # Splice the title and contents sections into the template.
    sed -n -e "1,/<!-- BEGIN $TITLE -->/p" $template                         > $TMP
    sed -n -e "/<!-- BEGIN $TITLE -->/,/<!-- END $TITLE -->/p" $data        >> $TMP
    sed -n -e "/<!-- END $TITLE -->/,/<!-- BEGIN $CONTENTS -->/p" $template >> $TMP
    sed -n -e "/<!-- BEGIN $CONTENTS -->/,/<!-- END $CONTENTS -->/p" $data  >> $TMP
    sed -n -e "/<!-- END $CONTENTS -->/,\$p" $template                      >> $TMP
    mv -f $TMP $result

    # Remove any unwanted generic template sections
    if grep -q -e "<!-- NO $SIDENAV -->" $data; then
	rmsection $result "$SIDENAV"
    fi
    if grep -q -e "<!-- NO $HIGHLIGHTS -->" $data; then
	rmsection $result "$HIGHLIGHTS"
    fi

    # Let data files override normal template sections.
    if grep -q -e "<!-- NO $INST_ED -->" $data; then
	rmsection $result "$INST_ED"
    fi
    if grep -q -e "<!-- NO $INST_ABLE -->" $data; then
	rmsection $result "$INST_ABLE"
    fi
    if grep -q -e "<!-- NO $HTTP_LINK -->" $data; then
	rmsection $result "$HTTP_LINK"
    fi
    if grep -q -e "<!-- NO $FILE_LINK -->" $data; then
	rmsection $result "$FILE_LINK"
    fi
}


# makeinstalled $input_file $output_file
makeinstalled()
{
    rm -f $TMP.data
    cat >> $TMP.data <<EOF
<!-- BEGIN $TITLE -->
<TITLE>Installed Freeware</TITLE>
<META NAME="keywords"    CONTENT="SGI, IRIX, freeware, shareware, GNU, Linux">
<META NAME="description" CONTENT="Index of installed freeware packages">
<META NAME="owner"       CONTENT="freeware@sgi.com">
<!-- END $TITLE -->

<!-- BEGIN $CONTENTS -->
<H2>Installed Freeware</H2>
<UL>
EOF

    # Parse each release note and add an entry.  The entry will consist
    # of the title as represented in the <TITLE> identifier and a link
    # to the actual release note
    # first egrep -v is for files created by fw_common
    # second egrep -v is for files created by fw_latest
    # last egrep is to remove empty lines
    for FILE in `cd $RELDIR && \
		ls -1 *.html | \
		egrep -v "(installed|index|contributors|faq|fw|howto|index-alt|mirrors|legal_notice).html" | \
		egrep -v "index-(by-alpha|by-category|by-date|alt).html" | \
		egrep -v "^$" `
    do
	# sed -e 1q == head -1
	# sed -e 's/.*>\(.*\)>.*/\1/' == cut -f2- -d'>' | cut -f1 -d'<'
	#    to get the parts between <title>(I WANT THIS PART)</title>
	title=`grep -i '<TITLE>' $RELDIR/$FILE | \
		sed -e '1q' | \
		sed -e 's/.*>\(.*\)<.*/\1/' `
	echo "<LI><A HREF=\"$FILE\">$title</A>"		>>$TMP.data
    done

    # Create the footer information
    cat >> $TMP.data <<EOF
</UL>
<!-- END $CONTENTS -->
EOF

    mergetemplate $1 $TMP.data $2
    rm -f $TMP.data
}

#####################
## end subroutines ##
#####################


# Parse command line option; default to -installed
if [ "$1" = '' ] ; then
    MODE=-installed 
else
    MODE=$1;
fi

# default is to remove all index.html link sections
RMINSTED=true
RMINSTABLE=true
RMHTTPLINK=true
RMFILELINK=true
# default is to create only the index.html file
MKINSTALLED=false
# set command line option
case $MODE in 
    -installed)  ## default
	RMINSTED=false
	MKINSTALLED=true
	RMFILELINK=false
	RELDIR=${rbase:=/}/usr/freeware/relnotes
	;;
    -web)
	echo "                                       WEB"
	RMINSTABLE=false
	RMHTTPLINK=false
	RELDIR=.
	;;
esac

COMMON=$RELDIR/shared  ## MUST NOT CHANGE. same location as old fw_common
INDEX=$RELDIR/index.html
INST=$RELDIR/installed.html
TMP=/tmp/mkindex.$$

# Remove the previous index if one exists
# Create the index.html ($INDEX)
rm -f $INDEX
cp $COMMON/index.template $TMP.start
applyfilters $TMP.start
mv -f $TMP.start $INDEX

# Create installed.html ($INST)
if [ true = $MKINSTALLED ]; then
    rm -f $INST
    makeinstalled $COMMON/index.template $TMP.inst
    applyfilters $TMP.inst
    mv -f $TMP.inst $INST
fi

# Process data files
for file in $COMMON/*.data; do
    base=`basename $file .data`
    rm -f $RELDIR/$base.html
    mergetemplate $COMMON/index.template $file $TMP.$base
    applyfilters $TMP.$base
    mv -f $TMP.$base $RELDIR/$base.html
done

exit 0
